#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QTextStream>
#include <QStringList>
#include <QStringListIterator>

#include <cmath>

#include "../src/qgetopt.h"

QString help();
QString version();

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QGetOpt opts;

	opts.enableArguments(false);		// diasable arguments without option

// add possible options
	opts << QGetOpt::Option('a', "add", 1, -1) <<
			QGetOpt::Option('p', "pow", 2, 2) <<
			QGetOpt::Option('h', "help", 0, 0) <<
			QGetOpt::Option('v', "version", 0, 0);

// start parsing and checking
	if( !opts.parse() || !opts.check() )
	{
		qDebug() << "Error " << opts.lastError()->errorMessage();
		return 1;
	}

	QStringList values;
	QTextStream out(stdout);

	if( !opts.areArgumentsUsed() )
	{
		out << "Warning, no arguments at all\n";
	}
	else if( opts.optionValues('a', values) )
	{
		qint64 res = 0;
		QStringListIterator i(values);
		while( i.hasNext() )
			res += i.next().toLong();
		out << res << "\n";
	}
	else if( opts.optionValues("pow", values) )
	{
		qreal res;
		res = pow(values[0].toDouble(), values[1].toDouble());
		out << res << "\n";
	}
	else if( opts.findOpt('h')->used() )
	{
		out << help();
	}
	else if( opts.findOpt("version")->used() )
	{
		out << version();
	}

	return 0;
}

QString help()
{
	return "Simple program, that consts summ or power\n"
			"Used as an example of using QGetOpt calss\n"
			"use -a [num1, num2, ...] to add numbers\n"
			"use -p num1 num2 to count power\n";
}

QString version()
{
	return "version 0.01\n";
}
